package com.china08.yxyapi.util;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.Assert;

public class SendEmailUtil {

    /** 发送邮箱用户名 */
    public final static String USERNAME = "support@91yunxiao.com";
    /** 密码 */
    public final static String PASSWORD = "Bei002";
    /** 发送服务器 */
    public final static String MAIL_SERVER = "smtp.exmail.qq.com";
    /** 编码 */
    public final static String CODING = "UTF-8";

    /** 通知邮件邮箱配置 */
    public final static String NOTICE_MAIL_SERVER = "smtp.mxhichina.com";
    public final static int NOTICE_PORT = 465;
    public final static String NOTICE_USERNAME = "exception@i2do.cc";
    public final static String NOTICE_PASSWORD = "Abc123456";
    public final static String NOTICE_FORM = "exception@i2do.cc";

    /**
     * 发送邮件
     * 
     * @param to
     *            收件人
     * @param from
     *            发送人
     * @param subject
     *            发送邮件抬头
     * @param text
     *            发送有内容
     */
    public static void sendEmailValidate(String to, String from, String subject, String text) {
        if (from == null) {
            from = "support@91yunxiao.com";
        }
        try {
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
            MimeMessage mailMessage = senderImpl.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
            senderImpl.setHost(MAIL_SERVER);
            senderImpl.setUsername(USERNAME);
            senderImpl.setPassword(PASSWORD);
            senderImpl.setDefaultEncoding(CODING);
            senderImpl.getJavaMailProperties().setProperty("mail.smtp.auth", "true");
            messageHelper.setTo(to);
            messageHelper.setText(text);
            messageHelper.setFrom(from);
            messageHelper.setSubject(subject);
            senderImpl.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            Assert.isTrue(false, "发送邮件失败");
        }
    }

    public static void sendNoticeEmail(String to, String subject, String text) {
        try {
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
            MimeMessage mailMessage = senderImpl.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
            // senderImpl.setPort(NOTICE_PORT);
            senderImpl.setHost(NOTICE_MAIL_SERVER);
            senderImpl.setUsername(NOTICE_USERNAME);
            senderImpl.setPassword(NOTICE_PASSWORD);
            senderImpl.setDefaultEncoding(CODING);
            senderImpl.getJavaMailProperties().setProperty("mail.smtp.auth", "true");
            messageHelper.setTo(to);
            messageHelper.setText(text);
            messageHelper.setFrom(NOTICE_FORM);
            messageHelper.setSubject(subject);
            senderImpl.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            Assert.isTrue(false, "发送邮件失败");
        }
    }
}
